﻿using System;
using System.Collections.Generic;
using Vote.Business;
using Vote.DataEntity;

namespace Vote.Web.Models
{
    public static class ConvertToViewModelExtend
    {
        // 投票转换
        public static VoteViewModel ConverToViewModel(this GroupEntity model)
        {
            if (model == null) { return new VoteViewModel(); }

            var viewModel = new VoteViewModel()
            {
                GroupID = model.GroupID,
                VoteName = model.GroupName,
                MinSelectedCount = model.MinSelectedCount,
                MaxSelectedCount = model.MaxSelectedCount,
                GroupDesc = model.GroupDesc,
                VoteType = model.VoteType,
                VoteItemList = new List<VoteItemViewModel>(),
            };

            if (model.VoteItemList != null && model.VoteItemList.Count > 0)
            {
                viewModel.VoteItemList = model.VoteItemList.ConvertAll(V =>
                    new VoteItemViewModel()
                    {
                        ItemID = V.ItemID,
                        ItemName = V.ItemName,
                        ImageUrl = V.ImageUrl,
                        RelationVideo = V.RelationVideo,
                        Introduction = V.Desc,
                    });
            }

            return viewModel;
        }

        public static VoterViewModel ConvertToViewModel(this VoterDataEntity model)
        {
            if (model == null) { return new VoterViewModel(); }

            var viewModel = new VoterViewModel()
            {
                UserId = model.UserId,
                Createtime = model.Createtime.ToString("yyyy-MM-dd HH:mm"),
                Itemname = model.Itemname,
                Itemkey = model.Itemkey,
                Groupname = model.Groupname
            };

            return viewModel;
        }


        public static ActionListViewModel ConvertToViewModel(this List<ActionEntity> modelList)
        {
            if (modelList == null || modelList.Count == 0) { return new ActionListViewModel(); }
            var viewModelList = new ActionListViewModel();
            foreach (var actionModel in modelList)
            {
                var actionViewModel = new ActionViewModel()
                {
                    Id = actionModel.ActionID,
                    Actionname = actionModel.ActionName,
                    CreateTime = actionModel.CreatTime,
                    Begintime = actionModel.BeginTime,
                    Endtime = actionModel.EndTime,
                    VoteCount = actionModel.VoteCount,
                };

                viewModelList.Add(actionViewModel);
            }

            return viewModelList;
        }

        public static VoteResultListViewModel ConvertToViewModel(this ActionEntity model)
        {
            var viewModel = new VoteResultListViewModel()
            {
                ActionId = model.ActionID,
                ActionName = model.ActionName,
                BeginTime = model.BeginTime,
                EndTime = model.EndTime,
                GroupList = new List<GroupViewModel>(),
            };
            List<String> result = new List<String>();

            if (model.VoteList != null)
            {
                foreach (var vote in model.VoteList)
                {
                    var group = new GroupViewModel() { Groupname = vote.GroupName, VoteResult = new List<VoteItemResultViewModel>(), };
                    group.VoteResult = vote.VoteItemList.ConvertAll(V => new VoteItemResultViewModel()
                      {
                          ItemID = V.ItemID,
                          ItemName = V.ItemName,
                          VoteCount = V.VoteCount,
                      });
                    //var sortResult = from item in vote.VoteItemList orderby item.VoteCount descending select item;
                    //foreach (var item in sortResult)
                    //{
                    //    result.Add(item.ItemName + ":" + item.VoteCount);
                    //}
                    //group.Result = result.ToString();
                    viewModel.GroupList.Add(group);

                }
            }
            return viewModel;
        }


        public static ActionDetailsViewModel ConvertToViewModel2(this ActionEntity model)
        {
            var viewModel = new ActionDetailsViewModel()
            {
                ActionId = model.ActionID,
                ActionName = model.ActionName,
                ActionDesc = model.ActionDesc,
                Begintime = model.BeginTime,
                Endtime = model.EndTime,
                GroupList = new List<GroupViewModel>(),
            };

            if (model.VoteList != null)
            {
                foreach (var vote in model.VoteList)
                {
                    var group = new GroupViewModel()
                    {
                        Id = vote.GroupID,
                        Groupname = vote.GroupName,
                        GroupDesc = vote.GroupDesc,
                        MaxNum = vote.MaxSelectedCount,
                        MinNum = vote.MinSelectedCount,
                        VoteType = vote.VoteType,
                        VoteResult = new List<VoteItemResultViewModel>(),
                    };
                    group.VoteResult = vote.VoteItemList.ConvertAll(V => new VoteItemResultViewModel()
                    {
                        ItemID = V.ItemID,
                        ItemName = V.ItemName,
                        ImageUrl = V.ImageUrl,
                        Videourl = V.RelationVideo,
                        Desc = V.Desc,
                        VoteCount = V.VoteCount,
                    });
                    viewModel.GroupIds += group.Id.ToString() + ",";
                    viewModel.GroupList.Add(group);
                }
                viewModel.GroupIds = viewModel.GroupIds.TrimEnd(new char[] { ',' });
            }


            return viewModel;
        }

        public static ActionDetailsViewModel ConvertToViewModel3(this ActionEntity model)
        {
            if (model == null) { return new ActionDetailsViewModel(); }
            var viewModel = new ActionDetailsViewModel()
            {
                ActionId = model.ActionID,
                ActionName = model.ActionName,
                ActionDesc = model.ActionDesc,
                Begintime = model.BeginTime,
                Endtime = model.EndTime,
            };

            return viewModel;
        }

        /// <summary>
        /// 转化成业务层活动实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static ActionDataEntity ConvertToActionEntity(ActionDetailsViewModel model)
        {
            ActionDataEntity entity = new ActionDataEntity()
            {
                ID = model.ActionId,
                GroupIds = model.GroupIds,
                ActionName = model.ActionName,
                ActionDesc = model.ActionDesc,
                BeginTime = model.Begintime,
                EndTime = model.Endtime,
                Group = new List<GroupDataEntity>(),
            };

            if (null != model.GroupList)
            {
                foreach (var item in model.GroupList)
                {
                    GroupDataEntity group = new GroupDataEntity()
                    {
                        GroupName = item.Groupname,
                        MinNum = item.MinNum,
                        MaxNum = item.MaxNum,
                        VoteType = item.VoteType,
                        GroupDesc = item.GroupDesc,
                        Item = new List<ItemDataEntity>(),
                    };

                    for (int i = 0; i < item.VoteResult.Count; i++)
                    {
                        var votedata = item.VoteResult[i];
                        var vote = new ItemDataEntity()
                        {
                            ItemName = votedata.ItemName,
                            ItemKey = ((char)(i + 65)).ToString(),
                            ImageUrl = votedata.ImageUrl,
                            RelationVideo = votedata.Videourl,
                            Desc = votedata.Desc,
                        };
                        group.Item.Add(vote);
                    }


                    entity.Group.Add(group);
                }
            }
            return entity;
        }
    }
}